package com.tech.zhidengxian.config.cos;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.ObjectMetadata;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

@Slf4j
public class MyCosClient {
    private final CosProperties cosProperties;
    private final COSClient cosClient;

    public MyCosClient(CosProperties cosProperties,COSClient cosClient){
        this.cosProperties = cosProperties;
        this.cosClient = cosClient;
    }

    public boolean putFormFile(String key ,MultipartFile file){
        boolean result = true;
        try {
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentLength(file.getSize());
            objectMetadata.setContentType(file.getContentType());
            cosClient.putObject(cosProperties.getBucketName(),key,file.getInputStream(),objectMetadata);
        }catch (IOException e){
            log.error("uploadFile error");
            result = false;
            e.printStackTrace();
        }
        return result;

    }

    public boolean putFile(String key,String fileName ,InputStream inputStream){
        boolean result = true;
        try {
            ObjectMetadata objectMetadata = buildObjectMetadata(fileName);
            objectMetadata.setContentLength(inputStream.available());
            cosClient.putObject(cosProperties.getBucketName(),key,inputStream,objectMetadata);
        }catch (IOException e){
            log.error("uploadFile error");
            result = false;
            e.printStackTrace();
        }
        return result;

    }

    public boolean putLocalFile(String key, File file){
        boolean result = true;
        try {
            cosClient.putObject(cosProperties.getBucketName(),key,file);
        }catch (CosClientException e){
            log.error("uploadFile error");
            result = false;
            e.printStackTrace();
        }
        return result;

    }

    private ObjectMetadata buildObjectMetadata(String fileName) {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        if (StringUtils.isNotEmpty(suffix)) {
            objectMetadata.setContentType(parseContentType(suffix));
        }
        return objectMetadata;
    }

    public static String parseContentType(String filenameExtension) {
        if ("mp4".equalsIgnoreCase(filenameExtension)) {
            return "video/mpeg4";
        } else if ("pdf".equalsIgnoreCase(filenameExtension)) {
            return "application/pdf";
        } else if ("bmp".equalsIgnoreCase(filenameExtension)) {
            return "image/bmp";
        } else if ("gif".equalsIgnoreCase(filenameExtension)) {
            return "image/gif";
        } else if (!"jpeg".equalsIgnoreCase(filenameExtension) && !"jpg".equalsIgnoreCase(filenameExtension)) {
            if ("png".equalsIgnoreCase(filenameExtension)) {
                return "image/png";
            } else if ("html".equalsIgnoreCase(filenameExtension)) {
                return "text/html";
            } else if ("txt".equalsIgnoreCase(filenameExtension)) {
                return "text/plain";
            } else if ("vsd".equalsIgnoreCase(filenameExtension)) {
                return "application/vnd.visio";
            } else if (!"pptx".equalsIgnoreCase(filenameExtension) && !"ppt".equalsIgnoreCase(filenameExtension)) {
                if (!"docx".equalsIgnoreCase(filenameExtension) && !"doc".equalsIgnoreCase(filenameExtension)) {
                    return "xml".equalsIgnoreCase(filenameExtension) ? "text/xml" : "application/octet-stream";
                } else {
                    return "application/msword";
                }
            } else {
                return "application/vnd.ms-powerpoint";
            }
        } else {
            return "image/jpeg";
        }
    }

    public String getUrl(String key){
        URL url = cosClient.getObjectUrl(cosProperties.getBucketName(), key);
        if (url != null) {
            return url.toString();
        }
        return null;
    }
}
